<?php

namespace App\Models\Activity\Jd;

use Illuminate\Database\Eloquent\Model;

use Illuminate\Support\Facades\Redis as Redis;

class JdPrize extends Model
{

    protected $table = 'jd_prize';

    protected $primaryKey = 'id';

    public $timestamps = false;

    public $incrementing = false;

    public static function  getCache ( $id ) {

        $prize = Redis::get('G_WDH_JD_PRIZE_' . $id );

        if ( empty( $prize ) ) {

            $prize_data = self::where('activity_id',$id)->get()->toArray();
            if ( empty( $prize_data ) ) {
                return null;
            }

            foreach ( $prize_data as $item ) {

                $prize[] = [
                    'id' => $item['id'],
                    'activity_id' => $item['activity_id'],
                    'card_id' => $item['card_id'],
                    'card_name' => $item['card_name'],
                    'card_num' => $item['card_num'],
                ];

            }

            Redis::setex( 'G_WDH_JD_PRIZE_' . $id , 604800 , json_encode( $prize ));

        } else {
            $prize = json_decode( $prize , true );
        }

        return $prize;


    }

}